﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.Revit;
using YJKRevitCode.RevitDimensionUtility;
using SystemDiagramNew.Utils;

namespace YJKOpeningDetail
{
	internal class Component
	{
		public Parameter GetParameter(Element element, string strParamenter)
		{
			ParameterSet parameters = element.Parameters;
			foreach (object obj in parameters)
			{
				Parameter parameter = (Parameter)obj;
				if (parameter.Definition.Name == strParamenter)
				{
					return parameter;
				}
			}
			return null;
		}

		public void CreateDetailLine(XYZ ptFirst, XYZ ptSecond, XYZ vectorMove, View view, Line dimNewLine, bool ifMove = false)
		{
			try
			{
				XYZ xyz = ptFirst.Add(NumbericTools.mmToFeet(1.0) * vectorMove);
				DetailCurve detailCurve = this.m_doc.Create.NewDetailCurve(view, Line.CreateBound(ptFirst, xyz));
				XYZ xyz2 = ptSecond.Add(NumbericTools.mmToFeet(1.0) * vectorMove);
				DetailCurve detailCurve2 = this.m_doc.Create.NewDetailCurve(view, Line.CreateBound(ptSecond, xyz2));
				ReferenceArray referenceArray = new ReferenceArray();
				referenceArray.Append(new Reference(detailCurve));
				referenceArray.Append(new Reference(detailCurve2));
				if (ifMove)
				{
					dimNewLine = this.GetAMoveLine(dimNewLine, NumbericTools.mmToFeet(Component.m_dMoveValue));
				}
				DimensionType dimensionType = RevitDimensionUtiltyMethod.GetDimensionType(this.m_doc, DimensionTypeEnum.ScaleDimension, null);
				Dimension dimension = this.m_doc.Create.NewDimension(view, dimNewLine, referenceArray);
				if (dimensionType != null)
				{
					dimension.DimensionType = dimensionType;
				}
			}
			catch (Exception)
			{
			}
		}

		public Line GetAMoveLine(Line input, double moveValue)
		{
			XYZ xyz = new XYZ(input.Direction.Y, -input.Direction.X, input.Direction.Z);
			Transform transform = Transform.CreateTranslation(xyz.Multiply(moveValue));
			return input.CreateTransformed(transform) as Line;
		}

		public void LeaderDimension(XYZ ptDimPosion, XYZ ptDimPosionFir, string strFamText, string strLevelText, ViewPlan viewPlan)
		{
			TextNoteType textNoteType = PublicFunction.GetTextType("仿宋_3.5mm", this.m_doc);
			if (textNoteType == null)
			{
				textNoteType = this.m_doc.CreateTextNoteType(3.5, 0.7, "仿宋");
			}
			XYZ xyz = XYZ.BasisY * NumbericTools.mmToFeet(60.0);
			double num = this.CalculTextWidth(strLevelText, textNoteType);
            TextNote textNode = this.m_doc.GetTextNode(viewPlan, ptDimPosionFir.Subtract(xyz), XYZ.BasisX, XYZ.BasisY, num, (TextAlignFlags)64, strLevelText, textNoteType);
			this.m_doc.Regenerate();
			double num2 = textNode.Height * (double)viewPlan.Scale;
			XYZ origin = new XYZ(ptDimPosionFir.X, ptDimPosionFir.Y + num2, ptDimPosionFir.Z);
			double num3 = this.CalculTextWidth(strFamText, textNoteType);
            TextNote textNode2 = this.m_doc.GetTextNode(viewPlan, origin, XYZ.BasisX, XYZ.BasisY, num3, (TextAlignFlags)64, strFamText, textNoteType);
			this.m_doc.Regenerate();
			double num4 = 0.0;
			RevitElementExtension.GetTextNoteWidth(textNode2);
			RevitElementExtension.GetTextNoteWidth(textNode);
			XYZ xyz2 = textNode2.BaseDirection.Normalize();
			textNode2.UpDirection.Normalize();
			if (strLevelText.Equals(strFamText))
			{
				this.m_doc.Delete(textNode.Id);
			}
			num4 = Math.Max(num4, num);
			num4 = Math.Max(num4, num3);
			Line line = Line.CreateBound(ptDimPosion, ptDimPosionFir);
			this.m_doc.Create.NewDetailCurve(viewPlan, line);
			Line line2 = Line.CreateBound(ptDimPosionFir, ptDimPosionFir.Add(xyz2 * num4 * (double)viewPlan.Scale));
			this.m_doc.Create.NewDetailCurve(viewPlan, line2);
		}

		private double CalculTextWidth(string strFamText, TextNoteType textTye)
		{
			double num = textTye.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble() * 100.0 * (double)this.m_doc.ActiveView.Scale / 100.0;
			double num2 = textTye.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).AsDouble();
			double num3 = num * num2;
			return (double)strFamText.Length * num3 / (double)this.m_doc.ActiveView.Scale;
		}

		public void CteateDim(XYZ ptFir, XYZ ptSec, XYZ ptVector, ViewPlan viewPlan)
		{
			ptFir.DistanceTo(ptSec);
			Line dimNewLine = Line.CreateBound(ptFir, ptSec);
			this.CreateDetailLine(ptFir, ptSec, ptVector, viewPlan, dimNewLine, false);
		}

		public Dictionary<XYZ, DataWall> m_dicWallCurve;

		public Dictionary<XYZ, List<Curve>> m_dicCimbineGridCurve;

		public FamilyInstance m_familyInstance;

		public Wall wall;

		public Document m_doc;

		public ViewPlan m_viewPlan;

		public static double m_dMoveValue = 500.0;
	}
}
